//
//  ScenicDetailSectionView.swift
//  LeTu
//
//  Created byg1 on 2024/1/15.
//

import UIKit

class ScenicDetailSectionView: UIView {

    private var sectionItems = [StoryTypeModel]()
    var selectClassifyHandle: ((StoryTypeModel, Int)->Void)?
    
    var repeatHandle: AppSwitchValueChange? = nil {
        didSet {
            switchView.valueChangedHandle = repeatHandle
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        configUI()
    }
    func setList(classifyItems: [StoryTypeModel], index: Int) {
        self.sectionItems = classifyItems
        dataSource.titles = sectionItems.map({ LanguageManager.shared.currentLanguage == .zh ? $0.name : $0.englishName })
        classifySegmentedView.defaultSelectedIndex = index
        classifySegmentedView.reloadData()
        switchView.isEnabled = kUserManager.isVip
        switchView.isOn = AppAudioManager.shared.isAutoPlay
        switchTitleLabel.text = "连续播放".localized
    }
    
    // MARK: - configUI
    func configUI() {
        
        addSubview(switchView)
        addSubview(switchTitleLabel)
        addSubview(classifySegmentedView)
        
        switchView.snp.makeConstraints { make in
            make.right.equalTo(-16)
            make.width.equalTo(48)
            make.height.equalTo(26)
            make.centerY.equalToSuperview()
        }
        switchTitleLabel.snp.makeConstraints { make in
            make.right.equalTo(switchView.snp.left).offset(-6)
            make.centerY.equalToSuperview()
        }
        classifySegmentedView.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.height.equalTo(36)
            make.centerY.equalToSuperview()
            make.right.equalTo(switchTitleLabel.snp.left).offset(-12)
        }
        
    }
    private(set) lazy var switchView: AppCustomSwitch = {
        let switchView = AppCustomSwitch()
        var config = AppSwitchConfig()
        config.offBgColor = UIColor.hex(hexString: "D9D9D9")
        config.onBgColor = .app_themeColor
        switchView.config = config
        return switchView
    }()
    private lazy var switchTitleLabel: UILabel = {
        let switchTitleLabel = UILabel()
        switchTitleLabel.font = kSetPingFangRegular(12)
        switchTitleLabel.textColor = UIColor.hex(hexString: "252A35")
        return switchTitleLabel
    }()
    private lazy var sliderView: JXSegmentedIndicatorGradientLineView = {//标题背景
        let sliderView = JXSegmentedIndicatorGradientLineView()
        sliderView.colors =  [UIColor(red: 1, green: 0.839, blue: 0, alpha: 1), UIColor(red: 0.851, green: 0.851, blue: 0.851, alpha: 0)]
        return sliderView
    }()
    lazy var dataSource: JXSegmentedTitleDataSource = {
        let ds = JXSegmentedTitleDataSource()
        ds.titleNormalColor =  .app_textGrayColor
        ds.titleSelectedColor = .app_black
        ds.titleNormalFont = kSetPingFangMedium(14)
        ds.titleSelectedFont = kSetPingFangMedium(14)
        ds.itemSpacing = 30
        ds.isItemSpacingAverageEnabled = false
        return ds
    }()
    private(set) lazy var classifySegmentedView: JXSegmentedView = {
        let sv = JXSegmentedView(frame: CGRect.zero)
        sv.backgroundColor = .clear
        sv.dataSource = dataSource //数据源
        sv.indicators = [sliderView] //标题下的横线
        sv.contentEdgeInsetLeft = 0
        sv.contentEdgeInsetRight = 0
        sv.delegate = self //设置代理
        return sv
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension ScenicDetailSectionView: JXSegmentedViewDelegate {
    
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
        guard let handle = selectClassifyHandle else { return }
        
        handle(sectionItems[index], index)
    }
    
}

